package jude.java.money.scan;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import jude.java.framework.thread.ThreadPool;
import jude.java.money.beans.ScanFtp;
import jude.java.money.beans.Site;
import jude.java.money.provide.Provider;
import jude.java.money.provide.impl.ScanFtpPassportProvider;
import jude.java.money.provide.impl.ScanFtpPortProvider;
import jude.java.money.service.GlobalService;
import jude.java.money.spring.Services;
import jude.java.util.Utils;

public class Scanner {

	private static final ArrayList<String> IGNORE_SITE = new ArrayList<String>();
	static {
		IGNORE_SITE.add(".google.");
		IGNORE_SITE.add(".taobao.com");
		IGNORE_SITE.add(".qq.com");		//qzone
		IGNORE_SITE.add(".edu.cn");		//
		IGNORE_SITE.add(".baidu.com");	//
		IGNORE_SITE.add(".javaeye.com");
		IGNORE_SITE.add(".blogspot.com");
		IGNORE_SITE.add(".163.com");
		IGNORE_SITE.add(".news.cn");
		IGNORE_SITE.add(".ifeng.com");
		IGNORE_SITE.add(".sina.");
		IGNORE_SITE.add(".alibaba.com");
		IGNORE_SITE.add(".lotour.com");
		IGNORE_SITE.add(".livejournal.com");
		IGNORE_SITE.add(".yahoo.");
		IGNORE_SITE.add(".sohu.com");
		IGNORE_SITE.add(".zol.com.cn");
	}
	
	public static void main(String[] args) throws InterruptedException {
		Utils.startSystem();
//		scanSiteBySearch();
//		scanSites();
//		scanSites2();
//		scanSites3();
//		scanFtpPort();
		scanFtpProfile();
//		ignoreSites();
		System.out.println("end task");
	}
	
	static void scanFtpProfile() throws InterruptedException {
		int thread = 50;
		ThreadPool pool = new ThreadPool(thread, false);
		Provider<ScanFtp> provider = new ScanFtpPassportProvider(0);
		for (int i = 0; i < thread; i++) {
			PassportScaner scanner = new PassportScaner(provider, 0);
			pool.execute(scanner);
		}
		pool.waitAllTask();
	}
	
	static void scanFtpPort() throws InterruptedException {
		int thread = 10;
		ThreadPool pool = new ThreadPool(thread, false);
		Provider<Site> provider = new ScanFtpPortProvider();
		for (int i = 0; i < thread; i++) {
			FtpScaner scanner = new FtpScaner(provider);
			pool.execute(scanner);
		}
		pool.waitAllTask();
	}
	
	static void scanSites2() throws InterruptedException {
		int thread = 20;
		ThreadPool pool = new ThreadPool(thread, false);
//		Provider<Site> provider = new SimpleProvider<Site>();
		for (int i = 0; i < thread; i++) {
			SiteScaner scanner = new SiteScaner();
			pool.execute(scanner);
		}
		pool.waitAllTask();
	}
	
	static void scanSites3() {
		new YahooBacklinkSiteScanner("http://siteexplorer.search.yahoo.com/search?p=www.uggs-sale.us&fr=sfp&bwm=i").run();
	}
	
	static void scanSiteBySearch() throws InterruptedException {
		String[] keywords = StringUtils.split("ftp,game,wow,hot,hotel,wheat,机械,动力,医疗,英语,学习,游戏,魔兽世界,艳照,写真,出国", ',');
		int thread = 20;
		ThreadPool pool = new ThreadPool(thread, false);
//		Provider<Site> provider = new SimpleProvider<Site>();
		for (int i = 0; i < keywords.length; i++) {
//			SiteScaner scanner = new SiteScaner(SiteScaner.googleSearch(keywords[i]));
			SiteScaner scanner = new SiteScaner(SiteScaner.baiduSearch(keywords[i]));
			pool.execute(scanner);
		}
		pool.waitAllTask();
	}
	
	static void scanSites() throws InterruptedException {
		String[] urls = {
//				"http://www.xxx.com/",
				"http://www.javaeye.com/",
				"http://www.csdn.net/",
				"http://www.android.com/",
		};
		int thread = 2;
		ThreadPool pool = new ThreadPool(thread, false);
//		Provider<Site> provider = new SimpleProvider<Site>();
		for (int i = 0; i < urls.length; i++) {
			SiteScaner scanner = new SiteScaner(urls[i]);
			pool.execute(scanner);
		}
		pool.waitAllTask();
	}
	
	static void ignoreSites() {
		GlobalService service = Services.getGlobalService();
		StringBuilder hql = new StringBuilder();
		hql.append("update ").append(Site.class.getName());
		hql.append(" set status=").append(Site.STATUS_IGNORED);
		hql.append(" where status=0 and host like '%");
		int len = hql.length();
		List<String> ignoreSites = IGNORE_SITE;
		int count = 0;
		for (String site : ignoreSites) {
			hql.setLength(len);
			hql.append(site);
			if (site.charAt(site.length() - 1) == '.')
				hql.append('%');
			hql.append('\'');
			count += service.update(hql.toString());
		}
		System.out.println(count);
	}
}
